package com.atguigu.gmall.mq.receiver;

import com.rabbitmq.client.Channel;
import lombok.SneakyThrows;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

/**
 * @author mgx
 * @version 1.0
 * @description: TODO
 * @date 2022-12-14 20:20
 */
@Component
public class ConfirmReceiver {

    @SneakyThrows
    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue(value = "queue.confirm", durable = "true", autoDelete = "false"),
                    exchange = @Exchange(value = "exchange.confirm", durable = "true", autoDelete = "false"),
                    key = {"routingKey.confirm"}
            )
    })
    public void process(String msg, Message message, Channel channel) {
        System.out.println("消息是: " + msg);
        System.out.println("消息的唯一标识是: " + message.getMessageProperties().getDeliveryTag());
        System.out.println("消息主体是：" + new String(message.getBody()));
        // 参数一：消息的唯一标识，参数二： 是否批量确认
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }
}
